package com.kede.app.service.impl;

import com.kede.app.domain.RecyAreaPhone;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.RecyAreaPhoneRepository;
import com.kede.app.service.RecyAreaPhoneService;
import com.kede.app.service.dto.RecyAreaPhoneDto;
import com.kede.app.service.dto.RecyAreaPhoneQueryCriteria;
import com.kede.app.service.mapstruct.RecyAreaPhoneMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-07-01
**/
@Service
@RequiredArgsConstructor
public class RecyAreaPhoneServiceImpl implements RecyAreaPhoneService {

    private final RecyAreaPhoneRepository recyAreaPhoneRepository;
    private final RecyAreaPhoneMapper recyAreaPhoneMapper;

    @Override
    public Map<String,Object> queryAll(RecyAreaPhoneQueryCriteria criteria, Pageable pageable){
        Page<RecyAreaPhone> page = recyAreaPhoneRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recyAreaPhoneMapper::toDto));
    }

    @Override
    public List<RecyAreaPhoneDto> queryAll(RecyAreaPhoneQueryCriteria criteria){
        return recyAreaPhoneMapper.toDto(recyAreaPhoneRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecyAreaPhoneDto findById(Integer id) {
        RecyAreaPhone recyAreaPhone = recyAreaPhoneRepository.findById(id).orElseGet(RecyAreaPhone::new);
        ValidationUtil.isNull(recyAreaPhone.getId(),"RecyAreaPhone","id",id);
        return recyAreaPhoneMapper.toDto(recyAreaPhone);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecyAreaPhoneDto create(RecyAreaPhone resources) {
        return recyAreaPhoneMapper.toDto(recyAreaPhoneRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecyAreaPhone resources) {
        RecyAreaPhone recyAreaPhone = recyAreaPhoneRepository.findById(resources.getId()).orElseGet(RecyAreaPhone::new);
        ValidationUtil.isNull( recyAreaPhone.getId(),"RecyAreaPhone","id",resources.getId());
        recyAreaPhone.copy(resources);
        recyAreaPhoneRepository.save(recyAreaPhone);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            recyAreaPhoneRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecyAreaPhoneDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecyAreaPhoneDto recyAreaPhone : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("地区编号", recyAreaPhone.getAreaCode());
            map.put("联系电话", recyAreaPhone.getPhone());
            map.put("联系人", recyAreaPhone.getPhoneName());
            map.put(" createTime",  recyAreaPhone.getCreateTime());
            map.put(" updateTime",  recyAreaPhone.getUpdateTime());
            map.put(" createBy",  recyAreaPhone.getCreateBy());
            map.put(" updateBy",  recyAreaPhone.getUpdateBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}